// criado por Douglas de Oliveira Marque
package GeraGraficos;

import org.jfree.ui.RectangleInsets;
import java.awt.Color;
import java.util.ArrayList;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

// classe que realiza a criacao dos graficos
public class CriaGrafico {
	private XYSeriesCollection dataset = new XYSeriesCollection();
	// classe que cria grafico
	public JFreeChart createChart(String nomeGrafico, String nomeEixoY) {
		
		
		
        // create the chart...
        JFreeChart chart = ChartFactory.createXYLineChart(
            nomeGrafico,      // chart title
            "Tempo",                      // x axis label
            nomeEixoY,                      // y axis label
            dataset,                  // data
            PlotOrientation.VERTICAL,
            true,                    // include legend
            true,                     // tooltips
            false                     // urls
        );

        // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
        chart.setBackgroundPaint(Color.white);
        
        // get a reference to the plot for further customisation...
        XYPlot plot = chart.getXYPlot();
        plot.setBackgroundPaint(Color.white);
        // offset do grafico em relacao a tela
        plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
        
       // plot.setDomainGridlinePaint(Color.white);
       // plot.setRangeGridlinePaint(Color.white);
       
        // faz plotar continuo
        XYItemRenderer r = plot.getRenderer();
        //StandardXYItemRenderer renderer = (StandardXYItemRenderer) plot.getRenderer();
        
           XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r;
            renderer.setShapesVisible(true);// alterado para falso
           renderer.setShapesFilled(true); // true-false
            
            //seta os shapes 
            //renderer.setShape(ShapeUtilities.createDiamond(3));
        
        
        // change the auto tick unit selection to integer units only...
       NumberAxis domainAxis = (NumberAxis) plot.getDomainAxis();
        domainAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        domainAxis.setAutoRangeIncludesZero(true);  
        NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        rangeAxis.setAutoRangeIncludesZero(true);         
        // OPTIONAL CUSTOMISATION COMPLETED.   
        return chart;        
    }
	
	// metodo que le valores passados de valor e tempo e cria serie para grafico
	public XYSeries createSerie(int[] valor, int[] tempo, String nomeSerie) 
     {	         
         XYSeries series = new XYSeries("nomeSerie", false, true);
         for (int c=0 ; c < valor.length ; c++) 
         series.add(tempo[c], valor[c]);
         return(series);
     }
	// classe q recebe um arraycolleciton
	public XYSeries createSerie( ArrayList<Integer> valor,ArrayList<Integer> tempo,String nomeSerie)
	 { 
		 XYSeries series = new XYSeries(nomeSerie, false, true);
		 for (int c=0; c<((tempo.size()));c++)
		 {
			 series.add(tempo.get(c),valor.get(c));
		 }
    	return (series);
	 }
	
	
	// classe q adicona serie ao dataset
	public void insereData(XYSeries serie){
		dataset.addSeries(serie);
	}
	public void insereDataCriandoSerie(ArrayList<Long> valor,ArrayList<Integer> tempo, String nomeSerie){
		XYSeries series = new XYSeries(nomeSerie, false, true);
        for (int c=0 ; c < valor.size() ; c++) 
        series.add(tempo.get(c), valor.get(c));
        dataset.addSeries(series);
	}

	public void insereDataCriandoSerie(int[] valor, int[] tempo, String nomeSerie){
		XYSeries series = new XYSeries(nomeSerie, false, true);
        for (int c=0 ; c < valor.length ; c++) 
        series.add(tempo[c], valor[c]);
        dataset.addSeries(series);
	}
}